<?php
class template extends Smarty {
	public $ob;
	public $result;
	public function __construct(){
		$this->Smarty();
	}
	
	public function getTemplateVariable($params){
		$method = 'getTemplateVariable'.ucfirst($params['type']);
		if (method_exists(get_class($this),$method))
		{
			$method = 'getTemplateVariable'.ucfirst($params['type']);
			$value = $this->$method($params);
			$this->assign($params['var'],$value);
		}
		return false;
	}
	
	public function getTemplateVariableClassify($params){
		$db = base::db();
		$result = $db->fetchAll($db->select()->from("my_classify"));
		foreach($result as $val){
			if ('0' == $val['pid']){
				$value['p'][$val['cid']] = $val;
			} else {
				$value['s'][$val['pid']][] = $val;
			}
			$value['c'][$val['cid']] = $val;
		}
		return $value;
	}
	
	public function getTemplateVariablePrintClass($params)
	{
		$columns = $this->_tpl_vars['columns'];
		$word = $params['word'];
		$percent = array();
		foreach ($columns['id'] as $val)
		{
			similar_text($word,$val['classname'],$per);
			$percent[$val['classid']] = $per;
		}
		natsort($percent);
		$c = array_keys($percent);
		$num = count($c);
		$cl = $column = $columns['id'][$c[($num-1)]];
		
		$link = $ext = null;
		do {
			if (empty($ext)) 
			{
				$ext = empty($column['contentextension']) ? null : $column['contentextension'];
			}
			$link = empty($column['domain']) ? $column['classpath'].$link : $column['domain'].$link;
			$column = $this->_tpl_vars['class']['id'][$column['bclassid']];
		}
		while($column['bclassid']);
		
		$column = $cl;
		$column['url']	= $link;
		$column['name']	= $column['classname'];
		print $column[$params['field']];
	}
	
	public function getTemplateVariableSql($params){
		$db					= base::db();
		$where				= array();
		$from				= $params['from'];
		$fetchType			= $params['fetchType'];
		$params['where']	= explode(',',$params['where']);
		
		$select = $db->select();
		$select->from($from);
		foreach ($params['where'] as $val){
			list($k,$v)	= explode(':',$val);
			if (strstr($v,'@')){
				$v = str_replace('@','',$v);
				$v = $this->_tpl_vars[$v];
			}
			$select->where($k,$v);
		}
		if ($params['limit'])
		{
			list($offset,$limit) = explode(',',$params['limit']);
			$select->limitPage($offset,$limit);
		}
		$value = $db->$fetchType($select);
		return $value;
	}
	
	public function getTemplateVariableArg($params)
	{
		return $_REQUEST;
	}
	
	public function getTemplateVariableWebSite($params)
	{
		$db = base::db();
		$website = array();
		$result = $db->fetchAll("select * from my_website");
		foreach($result as $val)
		{
			$website['wid'][$val['wid']]   = $val;
			$website['cid'][$val['cid']][] = $val;
		}
		return $website;
	}
	
	public function getTemplateVariableClassbyword($params)
	{
		$class = $this->_tpl_vars['class'];
		$word = $params['word'];
		$percent = array();
		foreach ($class['id'] as $val)
		{
			similar_text($word,$val['classname'],$per);
			$percent[$val['classid']] = $per;
		}
		natsort($percent);
		$c = array_keys($percent);
		$num = count($c);
		$class = $class['id'][$c[($num-1)]];
		
		$class['url']	= empty($class['domain']) ? $class['classpath'] : $class['domain'];
		$class['name']	= $class['classname'];
		return $class;
	}
	
	public function getTemplateVariableArticle($params)
	{
		$db = base::db();
		if (isset($params['aid']))
		{
			$article = $db->fetchRow(
				"select * from my_article as a,my_article_data_1 as d
				where a.aid=d.aid and a.aid={$params['aid']}"
			);
			$class = $this->_tpl_vars['class']['id'][$article['classid']];
			
			$link = $ext = null;
			do {
				if (empty($ext)) 
				{
					$ext = empty($class['contentextension']) ? null : $class['contentextension'];
				}
				$link = empty($class['domain']) ? $class['classpath'].$link : $class['domain'].$link;
				$class = $this->_tpl_vars['class']['id'][$class['bclassid']];
			}
			while($class['bclassid']);
			$article['link'] = $link.'/'.$article['aid'].$ext;
			return $article;
		}
		else if (isset($params['classid']))
		{
			$select = $db->select();
			$select->from("my_article as a");
			$select->from("my_article_data_1 as d",isset($params['content']) ? "*" : 'aid');
			$select->where("a.aid=d.aid and a.classid={$params['classid']}");
			isset($params['where']) && $select->where($params['where']);
			$select->order($params['order']);
			
			$select->limit($params['row']);

			$article = $db->fetchAll($select);
			$class = $this->_tpl_vars['class']['id'][$params['classid']];
			
			$link = $ext = null;
			do {
				if (empty($ext)) 
				{
					$ext = empty($class['contentextension']) ? null : $class['contentextension'];
				}
				$link = empty($class['domain']) ? $class['classpath'].$link : $class['domain'].$link;
				$class = $this->_tpl_vars['class']['id'][$class['bclassid']];
			}
			while($class['bclassid']);
			foreach ($article as $key =>$val)
			{
				$article[$key]['link'] = $link.'/'.$val['aid'].$ext;
			}
			return $article;
		}
		else if (isset($params['word']))
		{
			$columns = $this->_tpl_vars['columns'];
			$word = $params['word'];
			$percent = array();
			foreach ($columns['id'] as $val)
			{
				similar_text($word,$val['classname'],$per);
				$percent[$val['classid']] = $per;
			}
			natsort($percent);
			$c = array_keys($percent);
			$num = count($c);
			$classid = $c[($num-1)];
			$select = $db->select();
			$select->from("my_article as a");
			$select->from("my_article_data_1 as d",isset($params['content']) ? "*" : 'aid');
			$select->where("a.aid=d.aid and a.classid={$classid}");
			isset($params['where']) && $select->where($params['where']);
			$select->order($params['order']);
			
			$select->limit($params['row']);
			$article = $db->fetchAll($select);
			$class = $this->_tpl_vars['class']['id'][$classid];
			
			$link = $ext = null;
			do {
				if (empty($ext)) 
				{
					$ext = empty($class['contentextension']) ? null : $class['contentextension'];
				}
				$link = empty($class['domain']) ? $class['classpath'].$link : $class['domain'].$link;
				$class = $this->_tpl_vars['class']['id'][$class['bclassid']];
			}
			while($class['bclassid']);
			foreach ($article as $key =>$val)
			{
				$article[$key]['link'] = $link.'/'.$val['aid'].$ext;
			}
			return $article;
		}
	}
}